package com.ztc.ZtcRefundRecord.dao;

import com.aqgj.util.BaseDaoImpl;
import com.ztc.ZtcRefundRecord.bean.ZtcRefundRecord;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

/**
 *
 * @author 
 * @date
 */
@Repository("ZtcRefundRecordDao")
public class ZtcRefundRecordDao extends BaseDaoImpl<ZtcRefundRecord> {

    /**
     * 获取退款记录列表
     * @param pageNo  页码
     * @param pageSize 条数
     * @param type  公司类型
     * @param cid   公司ID
     * @return
     */
    public List getRecordList(int pageNo, int pageSize,Integer type,String cid) {
        String sql ="SELECT t.id,t.name,t.type,t.order_number,t.order_balance,t.money,t.add_time " +
                    "FROM " +
                    " ( " +
                    "SELECT r.id,CONCAT_WS('-',s1.area_city,s.area_city) name,r.type,o.order_number,o.order_balance,r.money,r.add_time " +
                    "FROM ztc_refund_record r " +
                    "INNER JOIN ztc_order o ON r.fk_order_id = o.id " +
                    "INNER JOIN ztc_classes_site_for_sale s1 ON s1.id = o.fk_start_classes_site_for_sale_id " +
                    "INNER JOIN ztc_classes_site_for_sale s ON s.id = o.fk_end_classes_site_for_sale_id " +
                    "WHERE o.order_type in (1,3) UNION ALL " +
                    "SELECT r.id, CONCAT_WS('-',x.from_city,x.to_city) name,r.type,o.order_number,o.order_balance,r.money,r.add_time " +
                    "FROM ztc_refund_record r " +
                    "INNER JOIN ztc_order o ON r.fk_order_id = o.id " +
                    "INNER JOIN ztc_ctrip_data x ON x.fk_order_id=r.fk_order_id " +
                    "WHERE o.order_type in(2,4)) t";
        if (type != 0) {
            sql = "SELECT t.id,CONCAT_WS('-',s1.area_city,s.area_city) name,t.type,o.order_number,o.order_balance,t.money,t.add_time " +
                    " FROM ztc_refund_record t " +
                    " INNER JOIN ztc_order o ON t.fk_order_id = o.id " +
                    " INNER JOIN ztc_classes_site_for_sale s1 ON s1.id = o.fk_start_classes_site_for_sale_id " +
                    " INNER JOIN ztc_classes_site_for_sale s ON s.id = o.fk_end_classes_site_for_sale_id " +
                    " WHERE o.order_type=1 and o.fk_transport_id='" + cid + "'";
        }
        sql+=" order by t.add_time desc";
        return this.listSqlPagQuery(sql, pageNo, pageSize);
    }

    /**
     * 获取总条数
     * @param type 公司类型
     * @param cid  公司ID
     * @return
     */
    public  Integer getSqlSumQuery(Integer type,String cid){
        String sql ="SELECT r.id,c.name,r.type,o.order_number,o.order_balance,r.money,r.add_time " +
                "FROM ztc_refund_record r " +
                "INNER JOIN ztc_order o ON r.fk_order_id = o.id " +
                "INNER JOIN ztc_classes_for_sale c ON c.id = o.fk_classes_for_sale_id " +
                "WHERE o.order_type in(1,3) UNION ALL " +
                "SELECT r.id, CONCAT_WS('-',x.from_city,x.to_city) name,r.type,o.order_number,o.order_balance,r.money,r.add_time " +
                "FROM ztc_refund_record r " +
                "INNER JOIN ztc_order o ON r.fk_order_id = o.id " +
                "INNER JOIN ztc_ctrip_data x ON x.fk_order_id=r.fk_order_id WHERE o.order_type in(2,4) ";
        if(type!=0){
            sql = " SELECT r.id,c.name,r.type,o.order_number,o.order_balance,r.money,r.add_time " +
                    "FROM ztc_refund_record r " +
                    "INNER JOIN ztc_order o ON r.fk_order_id = o.id " +
                    "INNER JOIN ztc_classes_for_sale c ON c.id = o.fk_classes_for_sale_id " +
                    "WHERE o.order_type=1 and o.fk_transport_id='" + cid + "'";
        }
       return  this.getSqlSumQuery(sql);
    }

    /**
     * 退款记录详情.
     * @param rid  退款记录ID
     * @param type 订单类型（1直通车订单，2携程订单,3顺风车订单，4去哪儿）
     * @return
     */
    public Object[] getRecordInfo(String rid,Integer type) {
        String sql="";
        if(type==1||type==3){
            sql="SELECT concat_ws('-',s.area_city,s1.area_city) name,r.type,o.order_number,o.order_balance,o.pay_balance,o.pay_type,order_type,o.order_time,r.money,r.add_time " +
                    ",c.car_type,s.departure_time, IFNULL(u.account,a.name) personnel " +
                    "FROM ztc_refund_record r " +
                    "INNER JOIN ztc_order o ON r.fk_order_id = o.id " +
                    "INNER JOIN ztc_classes_for_sale c ON c.id = o.fk_classes_for_sale_id  " +
                    "INNER JOIN ztc_classes_site_for_sale s ON s.id=o.fk_start_classes_site_for_sale_id " +
                    "INNER JOIN ztc_classes_site_for_sale s1 ON s1.id=o.fk_end_classes_site_for_sale_id " +
                    "LEFT JOIN sys_user u ON r.fk_manager_id=u.id " +
                    "LEFT JOIN app_user a ON r.fk_manager_id=a.id " +
                    "WHERE r.id='"+rid+"'";
        }else{
            sql="SELECT CONCAT_WS('-',x.from_city,x.to_city) name,r.type,o.order_number,o.order_balance,o.pay_balance,o.pay_type,order_type,o.order_time,r.money,r.add_time" +
                    ",x.car_type,x.departure_date,ifnull(u.account,a.name) personnel " +
                    "FROM ztc_refund_record r " +
                    "INNER JOIN ztc_order o ON r.fk_order_id = o.id " +
                    "INNER JOIN ztc_ctrip_data x ON x.fk_order_id=r.fk_order_id " +
                    "LEFT JOIN sys_user u ON r.fk_manager_id=u.id " +
                    "LEFT JOIN app_user a ON r.fk_manager_id=a.id " +
                    "WHERE r.id='"+rid+"'";
        }
        return (Object[]) this.createSQLQuery(sql).getResultList().get(0);
    }
}